home *** CD-ROM | disk | FTP | other *** search
/ Clickx 47 / Clickx 47.iso / assets / software / SyncBack / VirtualBox_1.5.4_Win_x86.msi / apisamplefile next >
Encoding:
Text File  |  2007-12-29  |  4.2 KB  |  161 lines

  1. /** @file
  2.  *
  3.  * tstVBoxAPIWin - sample program to illustrate the VirtualBox
  4.  *                 COM API for machine management on Windows.
  5.                    It only uses standard C/C++ and COM semantics,
  6.  *                 no additional VBox classes/macros/helpers. To
  7.  *                 make things even easier to follow, only the
  8.  *                 standard Win32 API has been used. Typically,
  9.  *                 C++ developers would make use of Microsoft's
  10.  *                 ATL to ease development.
  11.  */
  12.  
  13. /*
  14.  * Copyright (C) 2006-2007 innotek GmbH
  15.  *
  16.  * This file is part of VirtualBox Open Source Edition (OSE), as
  17.  * available from http://www.virtualbox.org. This file is free software;
  18.  * you can redistribute it and/or modify it under the terms of the GNU
  19.  * General Public License as published by the Free Software Foundation,
  20.  * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
  21.  * distribution. VirtualBox OSE is distributed in the hope that it will
  22.  * be useful, but WITHOUT ANY WARRANTY of any kind.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include "VirtualBox.h"
  27.  
  28.  
  29. int listVMs(IVirtualBox *virtualBox)
  30. {
  31.     HRESULT rc;
  32.  
  33.     /*
  34.      * First we have to get a list of all registered VMs
  35.      */
  36.     IMachineCollection *collection = NULL;
  37.     IMachineEnumerator *enumerator = NULL;
  38.  
  39.     do
  40.     {
  41.         rc = virtualBox->get_Machines(&collection);
  42.         if (SUCCEEDED(rc))
  43.             rc = collection->Enumerate(&enumerator);
  44.  
  45.         if (SUCCEEDED(rc))
  46.         {
  47.             BOOL hasMore;
  48.             while (enumerator->HasMore(&hasMore), hasMore)
  49.             {
  50.                 /*
  51.                  * Get the machine object
  52.                  */
  53.                 IMachine *machine = NULL;
  54.                 rc = enumerator->GetNext(&machine);
  55.                 if (SUCCEEDED(rc))
  56.                 {
  57.                     BSTR str;
  58.  
  59.                     machine->get_Name(&str);
  60.                     printf("Name: %S\n", str);
  61.  
  62.                     SysFreeString(str);
  63.  
  64.                     machine->Release();
  65.                 }
  66.             }
  67.         }
  68.     } while (0);
  69.  
  70.     if (enumerator)
  71.         enumerator->Release();
  72.     if (collection)
  73.         collection->Release();
  74.  
  75.     return 0;
  76. }
  77.  
  78. int testErrorInfo(IVirtualBox *virtualBox)
  79. {
  80.     HRESULT rc;
  81.  
  82.     /* try to find a machine that doesn't exist */
  83.     IMachine *machine = NULL;
  84.     BSTR machineName = SysAllocString(L"Foobar");
  85.  
  86.     rc = virtualBox->FindMachine(machineName, &machine);
  87.  
  88.     if (FAILED(rc))
  89.     {
  90.         IErrorInfo *errorInfo;
  91.  
  92.         rc = GetErrorInfo(0, &errorInfo);
  93.  
  94.         if (FAILED(rc))
  95.             printf("Error getting error info! rc = 0x%x\n", rc);
  96.         else
  97.         {
  98.             BSTR errorDescription = NULL;
  99.  
  100.             rc = errorInfo->GetDescription(&errorDescription);
  101.  
  102.             if (FAILED(rc) || !errorDescription)
  103.                 printf("Error getting error description! rc = 0x%x\n", rc);
  104.             else
  105.             {
  106.                 printf("Successfully retrieved error description: %S\n", errorDescription);
  107.  
  108.                 SysFreeString(errorDescription);
  109.             }
  110.  
  111.             errorInfo->Release();
  112.         }
  113.     }
  114.  
  115.     if (machine)
  116.         machine->Release();
  117.  
  118.     SysFreeString(machineName);
  119.  
  120.     return 0;
  121. }
  122.  
  123.  
  124. int main(int argc, char *argv[])
  125. {
  126.     HRESULT rc;
  127.     IVirtualBox *virtualBox;
  128.  
  129.     do
  130.     {
  131.         /* initialize the COM subsystem */
  132.         CoInitialize(NULL);
  133.  
  134.         /* instantiate the VirtualBox root object */
  135.         rc = CoCreateInstance(CLSID_VirtualBox,       /* the VirtualBox base object */
  136.                               NULL,                   /* no aggregation */
  137.                               CLSCTX_LOCAL_SERVER,    /* the object lives in a server process on this machine */
  138.                               IID_IVirtualBox,        /* IID of the interface */
  139.                               (void**)&virtualBox);
  140.  
  141.         if (!SUCCEEDED(rc))
  142.         {
  143.             printf("Error creating VirtualBox instance! rc = 0x%x\n", rc);
  144.             break;
  145.         }
  146.  
  147.         listVMs(virtualBox);
  148.  
  149.         testErrorInfo(virtualBox);
  150.  
  151.         /* release the VirtualBox object */
  152.         virtualBox->Release();
  153.  
  154.     } while (0);
  155.  
  156.     CoUninitialize();
  157.     return 0;
  158. }
  159.  
  160.  
  161.